package com.lxl.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;

import com.lxl.model.BookUser;
import com.lxl.service.BookUserService;
import com.lxl.service.impl.BookUserServiceImpl;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Font;
import java.awt.Cursor;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @author 刘星鹭
 * @Email liuxinglumail@163.com
 * @version 创建时间： 2014年9月12日 下午3:17:34
 */

public class AddBookUser extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField username;
	private JTextField password;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddBookUser frame = new AddBookUser();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public AddBookUser() {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				ViewBookUser.main(new String[1]);
			}
		});
		setFont(new Font("微软雅黑", Font.PLAIN, 12));
		setTitle("图书管理系统");
		setBounds(100, 100, 450, 241);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);

		username = new JTextField();
		username.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		username.setBounds(131, 40, 265, 31);
		panel.add(username);
		username.setColumns(10);

		password = new JTextField();
		password.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		password.setColumns(10);
		password.setBounds(131, 94, 265, 31);
		panel.add(password);

		JLabel label = new JLabel("请输入新用户名：");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		label.setBounds(31, 48, 116, 15);
		panel.add(label);

		JLabel label_1 = new JLabel("请输入新用户密码：");
		label_1.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		label_1.setBounds(20, 102, 116, 15);
		panel.add(label_1);

		JButton button = new JButton("添加");
		button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		button.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				String name = username.getText();
				String psw = password.getText();
				if (name.equals("") || psw.equals(""))
					JOptionPane.showMessageDialog(null, "请填写所有资料！");
				else {
					BookUserService bookUserService = new BookUserServiceImpl();
					bookUserService.persist(new BookUser(name, psw));
					JOptionPane.showMessageDialog(null, "添加成功！");
				}
			}
		});
		button.setBounds(165, 147, 93, 23);
		panel.add(button);
	}
}
